<?php

/**
 * Created by PhpStorm.
 * User: wukent
 * Date: 2017/9/20
 * Time: 上午11:51
 */
class MerchantCommentModel extends Model
{
    public function numList($mid,$page="1",$pernum="5") {

        $where = array('merchant_id'=>$mid);
        $prev = ($page-1)*$pernum;
        // return "select m.*,u.nick_name,u.user_pic from manage_merchant_comment m JOIN ecs_users u ON m.user_id = u.user_id where merchant_id={$mid} ORDER BY datetime DESC limit {$prev},{$pernum}";die;
//        $data = $this
//
//            ->join('ecs_users ON ecs_users.user_id = '.C('DB_PREFIX').'merchant_comment.user_id')
//            ->where($where)
//            ->order('comment_id DESC')
//            ->page($page.','.$pernum)
//            ->field('comment_id,user_id,nick_name,merchant_id,grade,details,datetime,state')
//            ->select();
        $data = $this->query("select m.*,u.nick_name,u.user_pic from manage_merchant_comment m JOIN ecs_users u ON m.user_id = u.user_id where merchant_id={$mid} ORDER BY datetime DESC limit {$prev},{$pernum}");
        return $data;

    }

    public function num($mid,$page="1",$pernum="5") {

        $where = array('merchant_id'=>$mid);
        // return "select m.*,u.nick_name,u.user_pic from manage_merchant_comment m JOIN ecs_users u ON m.user_id = u.user_id where merchant_id={$mid} ORDER BY datetime DESC limit {$prev},{$pernum}";die;
//        $data = $this
//
//            ->join('ecs_users ON ecs_users.user_id = '.C('DB_PREFIX').'merchant_comment.user_id')
//            ->where($where)
//            ->order('comment_id DESC')
//            ->page($page.','.$pernum)
//            ->field('comment_id,user_id,nick_name,merchant_id,grade,details,datetime,state')
//            ->select();
        $data = $this->query("select m.*,u.nick_name,u.user_pic from manage_merchant_comment m JOIN ecs_users u ON m.user_id = u.user_id where merchant_id={$mid} ORDER BY datetime DESC");
        return $data;
    }

    public function getCount($map) {
//        if ($map['supervisor_name']) {
//            $where['supervisor_name'] = array('like','%'.$map["supervisor_name"].'%');
//        }

        $where['menus'] = array('eq',$map['menusid']);
        $data = $this
            ->where($where)
            ->order('comment_id DESC')
            ->count();
        return $data;
    }

    public function getInfo($id) {
        $where = array('comment_id'=>$id);
        $data = $this->where($where)->find();
        return $data;
    }

    public function entry($uid,$mid) {

        $edata['grade'] = I('post.grade');
        $edata['details'] = I('post.details');
        $edata['user_id'] = $uid;
        $edata['merchant_id'] = $mid;

        $edata['datetime'] = NOW_TIME;
        $addinfo = $this->add($edata);




        if (I('post.photo') != ''){
            $photos = explode(',',I('post.photo'));
        }


        $photomod = M('MerchantCommentPhoto');
        if ($addinfo>0 && count($photos)>0){
            $photodata['comment_id'] = $addinfo;
            foreach ($photos as $key=>$value){
                $photodata['path'] = $value;
                $photodata['datetime'] = time();

                $photomod->add($photodata);
            }
        }
        return $addinfo;

    }

}